package com.xul.yygh.hosp.controller.api;

import com.xul.hosp.common.helper.HttpRequestHelper;
import com.xul.hosp.common.util.MD5;
import com.xul.yygh.common.exception.YyghException;
import com.xul.yygh.common.result.Result;
import com.xul.yygh.common.result.ResultCodeEnum;
import com.xul.yygh.hosp.service.DepartmentService;
import com.xul.yygh.hosp.service.HospitalService;
import com.xul.yygh.hosp.service.HospitalSetService;
import com.xul.yygh.hosp.service.ScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import model.com.xul.yygh.model.hosp.Department;
import model.com.xul.yygh.model.hosp.Hospital;
import model.com.xul.yygh.model.hosp.Schedule;
import model.com.xul.yygh.vo.hosp.DepartmentQueryVo;
import model.com.xul.yygh.vo.hosp.DepartmentVo;
import model.com.xul.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author xul
 * @create 2021-07-10 22:07
 */
@Api(tags = "上传医院接口")
@RestController
@RequestMapping("api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 删除排班信息
     *
     * @param request
     * @return
     */
    @ApiOperation("删除排班信息")
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        // 获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 获取排序编号
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        // 签名校验
        String sign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    /**
     * 查询排班接口
     *
     * @param request
     * @return
     */
    @ApiOperation("查询排班接口")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        // 获取排班信息
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        // 获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 获取科室编号
        String depcode = (String) paramMap.get("depocde");
        // 获取当前页
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        // 获取每页记录数
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        // 签名校验
        String sign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 调用service方法实现分页查询
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 上传排班信息
     *
     * @param request
     * @return
     */
    @ApiOperation("上传排班信息")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 获取排班信息
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        // 签名校验
        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }

    /**
     * 删除科室信息
     *
     * @param request
     * @return
     */
    @ApiOperation("删除科室信息")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        String sign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    /**
     * 查询科室信息
     *
     * @param request
     * @return
     */
    @ApiOperation("查询科室信息")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        // 获取科室的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取分页的当前页
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) (paramMap.get("page")));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        // 签名校验
        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 上传科室
     *
     * @param request
     * @return
     */
    @ApiOperation("上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 获得科室传来的信息
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        // 签名的检验
        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(paramMap);
        return Result.ok();

    }

    /**
     * 获取医院的信息
     *
     * @param request
     * @return
     */
    @ApiOperation("获取医院的信息")
    @PostMapping("hospital/show")
    public Result Hospital(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) paramMap.get("hoscode");
        // 签名的检验
        String sign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 上传医院接口
     *
     * @param request 这个是医院管理的post请求来的数据
     * @return
     */
    @ApiOperation("上传医院接口")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        // 获取医院请求过来的数据
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        // 经行加密 1获取医院传过来的签名,该签名已经经行了md5加密
        String sign = (String) paramMap.get("sign");
        // 2 根据医院传递过来的医院编号 查询数据库 查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String SignKey = hospitalSetService.getSignKey(hoscode);
        // 3 把数据库的签名经行md5加密
        String SignKeyMd5 = MD5.encrypt(SignKey);
        // 4 判断签名是否一样的
        if (!sign.equals(SignKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        // 调用service方法
        hospitalService.save(paramMap);
        return Result.ok();
    }

}
